package cn.iocoder.yudao.module.iot.controller;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.iot.controller.admin.deviceeventslog.vo.DeviceEventsLogExportReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.devicerel.vo.DeviceRelPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.devicerel.vo.DeviceRelRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.devicerel.vo.DeviceRelSaveReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.phonemsglog.vo.PhoneMsgLogSaveReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.warnpolicy.vo.WarnPolicyPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.warnpolicy.vo.WarnPolicySaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.deviceeventslog.DeviceEventsLogDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicerel.DeviceRelDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.phonemsglog.PhoneMsgLogDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.warnpolicy.WarnPolicyDO;
import cn.iocoder.yudao.module.iot.enums.Channel;
import cn.iocoder.yudao.module.iot.service.deviceeventslog.DeviceEventsLogService;
import cn.iocoder.yudao.module.iot.service.devicerel.DeviceRelService;
import cn.iocoder.yudao.module.iot.service.phonemsglog.PhoneMsgLogService;
import cn.iocoder.yudao.module.iot.service.warnpolicy.WarnPolicyService;
import cn.iocoder.yudao.module.iot.util.AmqpDataDownUtil;
import cn.iocoder.yudao.module.iot.util.SingleCallByTts;
import cn.iocoder.yudao.module.iot.vo.*;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.utils.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@RestController
@RequestMapping("/admin-api/deviceBind")
@Tag(name = "iot模块")
@Validated
public class IotDeviceBindController {

    private final static Logger logger = LoggerFactory.getLogger(IotDeviceBindController.class);


    @Resource
    private DeviceRelService deviceRelService;


    // 设备绑定
    @Operation(summary = "新增iot设备和燃气阀绑定")
    @RequestMapping("bindDevice")
    @ResponseBody
    public Object bindDevice(@RequestParam("deviceName") String deviceName,@RequestParam("valveCode") String valveCode){
        DeviceRelSaveReqVO rel=new DeviceRelSaveReqVO();
        rel.setDeviceName(deviceName);
        rel.setValveCode(valveCode);
        if(StringUtils.isEmpty(deviceName)){
            return error(1,"控制器编号为空！");
        }
        if(StringUtils.isEmpty(valveCode)){
            return error(1,"燃气阀编号为空！");
        }
        Long deviceCountByDeviceName = deviceRelService.getDeviceCountByDeviceName(deviceName);
        if(deviceCountByDeviceName>0){
            return error(301,"控制器已存在，请勿重复绑定！");
        }
        Long deviceCountByvalveCode = deviceRelService.getDeviceCountByvalveCode(valveCode);
        if(deviceCountByvalveCode>0){
            return error(302,"燃气阀已存在，请勿重复绑定！");
        }
        Integer deviceRel = deviceRelService.createDeviceRel(rel);
        return success(deviceRel);
    }


    @Operation(summary = "修改iot设备和燃气阀绑定")
    @RequestMapping("updateBindDevice")
    @ResponseBody
    public Object updateBindDevice( @RequestParam("id") Integer id,
                                    @RequestParam("deviceName") String deviceName,
                                    @RequestParam("valveCode") String valveCode,
                                    @RequestParam("errorFlag") Boolean errorFlag){
        DeviceRelSaveReqVO rel=new DeviceRelSaveReqVO();
        rel.setId(id);
        rel.setDeviceName(deviceName);
        rel.setValveCode(valveCode);
        rel.setErrorFlag(false);
        deviceRelService.updateDeviceRel(rel);
        return success(true);
    }


    @Operation(summary = "删除iot设备和燃气阀绑定")
    @RequestMapping("delBindDevice")
    @ResponseBody
    public Object delBindDevice( @RequestParam("id") Integer id){
        deviceRelService.deleteDeviceRel(id);
        return success(true);
    }

    @Operation(summary = "批量删除iot设备和燃气阀绑定")
    @RequestMapping("batchDelBindDevice")
    @ResponseBody
    public Object batchDelBindDevice(@RequestBody List<Integer> ids){
        deviceRelService.batchDeleteDeviceRel(ids);
        return success(true);
    }

    @RequestMapping("/getBindDeviceByPage")
    @Operation(summary = "分页获取绑定的设备和燃气阀列表")
    //@PreAuthorize("@ss.hasPermission('iot:device-rel:query')")
    public CommonResult<PageResult<DeviceRelDO>> getBindDeviceByPage(@Valid DeviceRelPageReqVO pageReqVO) {
        PageResult<DeviceRelDO> pageResult = deviceRelService.getDeviceRelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceRelDO.class));
    }


    @Operation(summary = "修改iot设备和燃气阀绑定")
    @RequestMapping("updateDeviceError")
    @ResponseBody
    public Object updateDeviceError( @RequestParam("id") Integer id,
                                    @RequestParam("errorFlag") Boolean errorFlag){
        DeviceRelSaveReqVO rel=new DeviceRelSaveReqVO();
        rel.setId(id);
        rel.setErrorFlag(errorFlag);
        deviceRelService.updateDeviceRel(rel);
        return success(true);
    }

}
